﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace DS2171Utils
{
   public class DllCaller
    {
       public DllCaller()
       {

       }

      
    #region hard way to import variable from unmanaged dll
    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi)]
    internal static extern IntPtr GetProcAddress(IntPtr hModule, string procName); // <<<
 
    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi)]
    internal static extern IntPtr LoadLibrary(string lpszLib); //<<<
 
    static float ReadsExecutionTime()
    {
        IntPtr hdl = LoadLibrary("cudalib.dll");
        if (hdl != IntPtr.Zero)
        {
            IntPtr addr = GetProcAddress(hdl, "sExecutionTime");
            if (addr != IntPtr.Zero)
            {
                //int value = Marshal.ReadInt32(addr);      // for integer types
                float[] managedArray = new float[1];        // single value
                Marshal.Copy(addr, managedArray, 0, 1);     // for other types
                return managedArray[0];
            }
        }
        return 0;
    }
    #endregion


    }//fC
}
